package com.qiangesoft.design.behavioral.strategy;

/**
 * 策略模式
 */
public class Strategy {

    public static void main(String[] args) {
        ProducerContext context = new ProducerContext();

        // 中国
        context.setProducer(new ChinaIphoneProducer());
        System.out.println("Made in " + context.produce().getProducer());

        // 越南
        context.setProducer(new VietnamIphoneProducer());
        System.out.println("Made in " + context.produce().getProducer());

        // 美国
        context.setProducer(new USAIphoneProducer());
        System.out.println("Made in " + context.produce().getProducer());
    }
}

/**
 * 1.策略接口
 * 此处：Iphone制造商
 */
interface IphoneProducer {
    /**
     * 生产
     */
    Iphone produce();
}

/**
 * 2.策略实现类
 * 中国制造商、越南制造商、美国制造商
 */
class ChinaIphoneProducer implements IphoneProducer {
    @Override
    public Iphone produce() {
        return new Iphone("China");
    }
}

class VietnamIphoneProducer implements IphoneProducer {
    @Override
    public Iphone produce() {
        return new Iphone("Vietnam");
    }
}

class USAIphoneProducer implements IphoneProducer {
    @Override
    public Iphone produce() {
        return new Iphone("USA");
    }
}

/**
 * 3.环境类
 */
class ProducerContext {

    private IphoneProducer producer;

    public void setProducer(IphoneProducer producer) {
        this.producer = producer;
    }

    public Iphone produce() {
        return producer.produce();
    }
}

/**
 * 苹果手机
 */
class Iphone {
    /**
     * 制造商
     */
    private String producer;

    public Iphone(String producer) {
        this.producer = producer;
    }

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }
}